/**
 * Title :	Property view
 * Description: Property editor
 * Version:	provider
 * Copyright (c) 2001-2008
 * Terms of use:see license agreement at http://treebolic.sourceforge.net/en/license.htm
 * Author: Bernard Bou
 * Company: bsys
 */
package bsys.propertyview;

import java.awt.Color;
import java.awt.Component;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.AbstractCellEditor;
import javax.swing.JButton;
import javax.swing.JColorChooser;
import javax.swing.JDialog;
import javax.swing.JTable;
import javax.swing.table.TableCellEditor;

/**
 * Color editor
 * 
 * @author Bernard Bou
 */
class ColorEditor extends AbstractCellEditor implements TableCellEditor
{
	private static final long serialVersionUID = 1L;

	/**
	 * Color being edited
	 */
	private Color theCurrentColor;

	/**
	 * Button (editor component)
	 */
	private JButton theButton;

	/**
	 * Color chooser
	 */
	private JColorChooser theColorChooser;

	/**
	 * Wrapping color dialog
	 */
	private JDialog theColorDialog;

	/**
	 * Constructor
	 */
	public ColorEditor()
	{
		theButton = new JButton();
		theButton.setBorderPainted(false);
		theButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				theColorChooser.setColor(theCurrentColor);
				theColorDialog.setVisible(true);

				// dialog returns control
				fireEditingStopped();
			}
		});

		// set up the dialog that the button brings up.
		theColorChooser = new JColorChooser();
		theColorDialog = JColorChooser.createDialog(theButton, Messages.getString("ColorEditor.0"), true, // modal //$NON-NLS-1$
				theColorChooser,
				// ok button handler
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						theCurrentColor = theColorChooser.getColor();
					}
				},
				// cancel button handler
				new ActionListener()
				{
					public void actionPerformed(ActionEvent e)
					{
						theCurrentColor = null;
					}
				});
	}

	// implement the one method defined by TableCellEditor.
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.table.TableCellEditor#getTableCellEditorComponent(javax.swing.JTable, java.lang.Object, boolean, int, int)
	 */
	public Component getTableCellEditorComponent(JTable table, Object thisValue, boolean isSelected, int row, int column)
	{
		theCurrentColor = (Color) thisValue;
		theButton.setBackground(theCurrentColor);
		return theButton;
	}

	// implement the one CellEditor method that AbstractCellEditor doesn't.
	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.swing.CellEditor#getCellEditorValue()
	 */
	public Object getCellEditorValue()
	{
		return theCurrentColor;
	}
}
